package com.chixing.clone;

public class CloneTest {
    public static void main(String[] args) throws CloneNotSupportedException {
        Dog dog = new Dog(1);
        Cat cat = new Cat(1,"jin");
        dog.setcat(cat);
        Dog dog2 = (Dog) dog.clone();
        //修改dog2的id
        dog2.setId(2);
        //修改后与原来的id比较
        System.out.println("dogId:"+dog.getId()+"--->dog2Id:"+dog2.getId());//dogId:1--->dog2Id:2
        //修改cat的id
        dog.getcat().setId(2);
        System.out.println("cat1Id:"+dog.getcat().getId()+"--->cat2Id:"+dog2.getcat().getId());//cat1Id:2--->cat2Id:2

        System.out.println("dog object same="+(dog==dog2));//false
        System.out.println("dog object equals="+(dog.equals(dog)));//true
        System.out.println("cat object same="+(dog.getcat()==dog2.getcat()));//true
        System.out.println("cat object equals="+(dog.getcat().equals(dog.getcat())));//true

/*
* 1、由System.out.println("dogId:"+dog.getId()+"--->dog2Id:"+dog2.getId());//dogId:1--->dog2Id:2 可以看出修改新克隆对象(dog2)的8种基本类型和String属性时,原属性不变，
2、由System.out.println("cat1Id:"+dog.getcat().getId()+"--->cat2Id:"+dog2.getcat().getId());//cat1Id:2--->cat2Id:2
* 可以看出修改被克隆对象(dog)所包含的对象属性(cat)时，新克隆对象(dog2)的属性(cat)也会改变。
3、由System.out.println("dog object same="+(dog==dog2));//false 可以看出克隆前后两个dog对象不是同一个对象
4、由System.out.println("cat object same="+(dog.getcat()==dog2.getcat()));//true 可以看出克隆前后cat对象为同一对象

*
* */
    }

}
